package com.potenza.edu.media.controller;


import com.potenza.edu.media.model.UploadChuckResult;
import com.potenza.edu.media.service.MediaUploadService;
import com.potenza.edu.media.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/media/upload")
@CrossOrigin
public class MediaUploadController {

    @Autowired
    MediaUploadService mediaUploadService;

    //before-send-file
    @RequestMapping("/register")
    public Result register(
            @RequestParam("fileMd5") String fileMd5,
            @RequestParam("fileName") String fileName,
            @RequestParam("fileSize") Long fileSize,
            @RequestParam("mimetype") String mimetype,
            @RequestParam("fileExt") String fileExt) {

        return mediaUploadService.register(fileMd5,fileName,fileSize,mimetype,fileExt);
    }

    @RequestMapping("/checkchunk")
    public UploadChuckResult checkchunk(@RequestParam("fileMd5") String fileMd5,
                                        @RequestParam("chunk") Integer chunk,
                                        @RequestParam("chunkSize") Integer chunkSize) {
        return mediaUploadService.checkchunk(fileMd5,chunk,chunkSize);
    }

    @RequestMapping("/uploadchunk")
    public Result uploadchunk(@RequestParam("file") MultipartFile file,
                                         @RequestParam("fileMd5") String fileMd5,
                                         @RequestParam("chunk") Integer chunk) {

        return mediaUploadService.uploadchunk(file,fileMd5,chunk);
    }

    @GetMapping("/mergechunks")
    public Result mergechunks(@RequestParam("fileMd5") String fileMd5,
                        @RequestParam("fileName") String fileName,
                        @RequestParam("fileSize") Long fileSize,
                        @RequestParam("mimetype") String mimetype,
                        @RequestParam("fileExt") String fileExt){
        //加上业务，老师上传的视频，老师ID
        return mediaUploadService.mergechunks(fileMd5,fileName,fileSize,mimetype,fileExt);
    }
}

